package com.kd.user.entity;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.SneakyThrows;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.annotation.KeySql;

import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component
@Data
@Table(name = "user")
@Api("用户实体")
public class User {
    private static final Logger logger = LoggerFactory.getLogger(User.class);
    @Id
    @KeySql(useGeneratedKeys = true)
    @ApiModelProperty(value = "ID", name = "id")
    private String id;
    @ApiModelProperty(value = "用户名", name = "username")
    private String username;
    @ApiModelProperty(value = "用户密码", name = "password")
    private String password;
    @ApiModelProperty(value = "名字", name = "name")
    private String name;
    @ApiModelProperty(value = "年龄", name = "age")
    private String age;
    @ApiModelProperty(value = "性别", name = "sex")
    private String sex;
    @ApiModelProperty(value = "生日", name = "birthday")
    private Date birthday;
    @ApiModelProperty(value = "创建日期", name = "createtime")
    private Date createtime;
    @Transient
    @ApiModelProperty(value = "备注", name = "note")
    private String note;

    public String getCreatetimeStr() {
        SimpleDateFormat f=new SimpleDateFormat("yyyy-MM-dd");
        String s="";
        if(createtime!=null) {
            s = f.format(createtime);}
        return s;
    }

    public void setCreatetimeStr(String createtimeStr) throws ParseException {
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
        try {
            if (createtimeStr != null && !"".equals(createtimeStr)) {
                this.createtime = f.parse(createtimeStr);
            }
        } catch (ParseException e) {
                this.createtime=f.parse("1990-01-01");
                logger.error(id+"创建时间日期转换错误");
        }

    }

    public String getBirthdayStr() {
        SimpleDateFormat f=new SimpleDateFormat("yyyy-MM-dd");
        String s="";
        if(birthday!=null) {
            s = f.format(birthday);}
        return s;
    }


    public void setbirthdayStr(String birthdayStr) throws ParseException {
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
        try {
            if (birthdayStr != null && !"".equals(birthdayStr)) {
                this.birthday = f.parse(birthdayStr);
            }
        } catch (ParseException e) {
            this.birthday=f.parse("1990-01-01");
            logger.error(id+"生日日期转换错误");
        }
    }
}
